package com.sugon.springboot.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    private static final String SECRET = "asurplus_secret";
    public static String createToken(Integer userId, String account, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        String token = JWT.create()
                // 添加头部
                .withHeader(map)
                // 放入用户的id
                .withAudience(String.valueOf(userId))
                // 可以将基本信息放到claims中
                .withClaim("account", account)
                .withClaim("password", password)
                // 超时设置,设置过期的日期
                .withExpiresAt(new Date(System.currentTimeMillis() + 3600L * 1000))
                .sign(Algorithm.HMAC256(SECRET));
        return token;
    }
}
